package com.example.wangtieying.firtest;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import im.fir.sdk.FIR;
import im.fir.sdk.VersionCheckCallback;

public class MainActivity extends AppCompatActivity {
    Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        context.startService(new Intent(context, DownloadService.class));
        checkUpdate();
    }

    public void checkUpdate() {
        FIR.checkForUpdateInFIR("2423b79740e70858dff41f10821be58d", new VersionCheckCallback() {
            @Override
            public void onSuccess(String versionJson) {
                Log.i("fir", "check from fir.im success! " + "\n" + versionJson);
                try {
                    Gson gson = new Gson();
                    UpdateInfo info = gson.fromJson(versionJson, UpdateInfo.class);
                    if (ActivityUtil.getVersionCode(context) < info.getVersion()) {
                        PromptManager.showUpdateDialog(context, info.getChangelog(), info.getInstall_url(), info.getName());
                    }
                }catch(JsonSyntaxException e)
                {

                }
            }

            @Override
            public void onFail(Exception exception) {
                Log.i("fir", "check fir.im fail! " + "\n" + exception.getMessage());
            }

            @Override
            public void onStart() {
                Toast.makeText(getApplicationContext(), "正在获取", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                Toast.makeText(getApplicationContext(), "获取完成", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
